package Day2025_03_04;

public class LeetCode5 {
    public String longestPalindrome(String s) {
        char[] ch = s.toCharArray();
        int n = ch.length;
        if (n == 1) return s;
        int start = 0;
        int maxLen = 0;
        for (int i = 0; i < n; i++) {
            int left = i, right = i;
            while (left >= 0 && right < n && ch[left] == ch[right]) {
                left--;
                right++;
            }
            if (right - left - 1 > maxLen) {
                maxLen = right - left - 1;
                start = left + 1;
            }
            left = i; right = i + 1;
            while (left >= 0 && right < n && ch[left] == ch[right]) {
                left--;
                right++;
            }
            if (right - left - 1 > maxLen) {
                maxLen = right - left - 1;
                start = left + 1;
            }
        }
        return s.substring(start, start + maxLen);
    }
}
